<template>
  <view>
    <ask-for-leave-list :status="status" :apply-list="applyList" :is-teacher="true"/>
  </view>
</template>

<script>
import askForLeaveList from "../../components/ask_for_leave_list/ask_for_leave_list";
export default {
  data() {
    return {
      applyList: [],
      status: '',
      page: 1,
      total: 0,
      clazzId: ''
    };
  },
  components: {
    askForLeaveList
  },
  onLoad(e) {
    this.clazzId = e.id;
    uni.$emit("setVuexUserInfo",{user:null,isLoginPage: false});
    uni.$on("changeAudit",this.changeAudit);
    this.initial();
  },
  onPullDownRefresh(){
    this.initial();
  },
  onReachBottom() {
    this.page = ++ this.page;
    this.status = 'loading';
    if((this.page - 1) * 5 > this.total) {
      setTimeout(() => {
        this.status = 'nomore';
      }, 1000)
    }else {
      this.getApplyList().then(res => {
        this.total = res.total;
        this.applyList = this.applyList.concat(res.data);
      });
      this.status = 'loadmore';
    }
  },
  methods:{
    initial(){
      this.page = 1;
      this.total = 0;
      this.getApplyList().then(res => {
        this.total = res.total;
        this.applyList = res.data;
        uni.stopPullDownRefresh();
      });
    },
    changeAudit({index,audit}){
      //更新请假信息的审核状态
      this.applyList[index].audit = audit;
      // 解决dom不更新问题
      this.$set(this.applyList,index,this.applyList[index])
    },
    async getApplyList(){
      let data = await this.$u.api.getAskForLeaveByClazzId(this.clazzId,{pageNum: this.page});
      return data;
    }
  }
}
</script>

<style lang="scss">

</style>
